#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (c) 2025 粤港澳大湾区（广东）国创中心

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

File: OnCataegoryFile.py
Desc: File operations pannel in the application.
Author: xumanjia
Date: 2025-08-01
"""
from PyQt5.QtWidgets import QFileDialog
from scripts.File.pyncti_import_file import pyncti_import_file
from scripts.File.pyncti_export_file import pyncti_export_file
from scripts.File.pyncti_new_document import pyncti_new_document
from scripts.File.pyncti_new_assembly import pyncti_new_assembly
from scripts.File.pyncti_add_assembly import pyncti_add_assembly
from scripts.File.pyncti_add_part import pyncti_add_part 

from scripts.File.pyncti_delete_part import pyncti_delete_part
from scripts.File.pyncti_delete_objects import pyncti_delete_objects
from scripts.File.pyncti_clear_doc import pyncti_clear_doc
from scripts.File.pyncti_close_doc import pyncti_close_doc
from scripts.File.pyncti_redo import pyncti_redo
from scripts.File.pyncti_undo import pyncti_undo
from scripts.File.pyncti_open_ncti_file import pyncti_open_ncti_file
from scripts.File.pyncti_save_file import pyncti_save_file
from Function.onCataegoryModel import on_visual_checkbox_body,on_visual_checkbox_vertex

def importFile(self):
    if False == hasattr(self, 'doc'):
        return        
    pyncti_import_file(self.NCTI,self.doc)
    self.updateTree()
        

def on_export_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_export_file(self.NCTI,self.doc)
    
def on_new_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    view = pyncti_new_document(self.NCTI,self.doc,self.hwnd)
    self.view = view
    self.selmode_body.setChecked(True)
    setcolor(self)
    on_visual_checkbox_body(self,False)
    on_visual_checkbox_vertex(self,False)
    self.cad_view.updateDoc(self)
    self.autoSizeView()

def on_new_assembly_button_click(self):
    if False == hasattr(self, 'doc'):
        return    
    view = pyncti_new_assembly(self.NCTI,self.doc,self.hwnd)   
    self.view = view 
    self.cad_view.updateDoc(self)
    self.autoSizeView()
    self.selmode_body.setChecked(True)
    setcolor(self)
    on_visual_checkbox_body(self,False)
    on_visual_checkbox_vertex(self,False)
    

def on_add_assembly_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    pyncti_add_assembly(self.NCTI,self.doc)
    self.autoSizeView()

def on_add_part_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    pyncti_add_part(self.NCTI,self.doc)
    self.autoSizeView()

def on_delete_part_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    pyncti_delete_part(self.NCTI,self.doc)

def on_script_run_button_click(self):
    if False == hasattr(self, 'doc'):
        return 
    self.updateTree()
    
def on_script_editor_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    
def on_console_button_click(self):
    if False == hasattr(self, 'doc'):
        return  

def on_delete_button_click(self):
    if False == hasattr(self, 'doc'):
        return      
    pyncti_delete_objects(self.NCTI,self.doc)
    self.updateTree()

def on_undo_button_click(self):
    if False == hasattr(self, 'doc'):
        return        
    pyncti_undo(self.NCTI,self.doc)
    self.updateTree()

def on_redo_button_click(self):
    if False == hasattr(self, 'doc'):
        return        
    pyncti_redo(self.NCTI,self.doc)
    self.updateTree()

def on_close_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    pyncti_close_doc(self.NCTI,self.doc)
    self.updateTree()

def on_clear_button_click(self):
    if False == hasattr(self, 'doc'):
        return  
    pyncti_clear_doc(self.NCTI,self.doc)
    self.updateTree()

def on_open_button_click(self):    
    view = pyncti_open_ncti_file(self.NCTI,self.doc,self.hwnd)   
    if view == None:
        return 
    else:
        self.view = view  
        setcolor(self)
        self.cad_view.updateDoc(self)   
        self.autoSizeView()

def on_save_button_click(self):
    if False == hasattr(self, 'doc'):
        return
    pyncti_save_file(self.NCTI,self.doc) 

def setcolor(self):
    if False == hasattr(self, 'doc') or self.doc.ID == -1:
        return  
    if False == hasattr(self, 'view'):
        return  
    seg = self.doc.Scene()
    modelcolor = self.NCTI.Color(145.0/255.0,158.0/255.0,186.0/255.0)
    backcolor = self.NCTI.Color(60.0/255.0,60.0/255.0,60.0/255.0)
    highlightcolor = self.NCTI.Color(245.0/255.0,128.0/255.0,15.0/255.0)
    vertexcolor = self.NCTI.Color(248.0/255.0, 203.0/255.0, 22.0/255.0)
    seg.SetFaceColor(modelcolor)
    seg.SetBackFaceColor(backcolor)
    seg.SetHighLightColor(highlightcolor)
    seg.SetHighContColor(highlightcolor)
    seg.SetMarkerColor(vertexcolor)
    self.view.SetBackgroundColor(5, 38, 79,81, 90, 146)